//
//  TopView.swift
//  ERecycle-Shop
//
//  Created by abb on 16/8/16.
//  Copyright © 2016年 abb. All rights reserved.
//

import UIKit

class TopView: UIView {
    
    var clickIconViewCallBack:(()->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        backgroundColor = UIColor.whiteColor()
    }
    /**
     加载ui
     */
    private func setupUI() {
        addSubview(iconBtn)
        addSubview(splitLine)
        addSubview(textfield)
        
        iconBtn.snp_makeConstraints { (make) in
            make.top.left.bottom.equalTo(self)
            make.width.equalTo(self.snp_height)
        }
        splitLine.snp_makeConstraints { (make) in
            make.left.equalTo(iconBtn.snp_right).offset(10)
            make.centerY.equalTo(self)
            make.height.equalTo(self).multipliedBy(0.5)
            make.width.equalTo(0.5)
        }
        textfield.snp_makeConstraints { (make) in
            make.left.equalTo(splitLine.snp_right).offset(10)
            make.top.right.bottom.equalTo(self)
        }
        
        iconBtn.addTarget(self, action: #selector(clickIconView), forControlEvents: .TouchUpInside)
    }
    
    func clickIconView() {
        clickIconViewCallBack!()
    }
    /**
     添加提醒
     */
    func addRemindView(title:String) {
        iconBtn.addSubview(remindView)
        remindView.setTitle(title, forState: .Normal)
        remindView.snp_makeConstraints { (make) in
            make.top.right.equalTo(iconBtn)
            make.size.equalTo(CGSize(width: 20, height: 20))
        }
    }
    /**
     移除提醒
     */
    func removeRemindView() {
        remindView.snp_removeConstraints()
        remindView.removeFromSuperview()
    }
    /// 头像按钮
    lazy var iconBtn:UIButton = {
        let btn = UIButton(imageName: "user_default", bgColor: UIColor.whiteColor())
        return btn
    }()
    /// 消息提醒圆圈
    lazy var remindView:UIButton = {
        let btn = UIButton(title: "0", titleColor: UIColor.whiteColor(), fontSize: 12, bgColor: UIColor.redColor())
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true
        return btn
    }()
    /// 分割线
    private lazy var splitLine:UIView = UIView.splitLine(UIColor.grayColor())
    /// 查询文本框
    lazy var textfield:UITextField = {
        let tf = UITextField()
        tf.placeholder = "搜索分类/关键字"
        tf.addTarget(nil, action: #selector(HomeViewController.clickTextField), forControlEvents:.TouchUpInside)
        tf.delegate = self
        return tf
    }()
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
extension TopView:UITextFieldDelegate {
    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        return false
    }
}
